//
//  ViewController.m
//  动画试验品
//
//  Created by rimi on 16/3/17.
//  Copyright © 2016年 ruifengpeixun. All rights reserved.
//

#import "LocationViewController.h"
#import "FitOfScreen.h"
#import "loadView.h"

@interface LocationViewController ()
@property(nonatomic,strong)CALayer *myLayer;
@property(nonatomic,strong)UIImageView *musicImage;
@property(nonatomic,assign)CGFloat leftY;
@property(nonatomic,assign)CGFloat rightY;
@property(nonatomic,assign)CGFloat curveX;
@property(nonatomic,assign)CGFloat curveY;
@property(nonatomic,strong)CAShapeLayer *maskLayer;
@property(nonatomic,strong)UIView *maskview;
@property(nonatomic,strong)CADisplayLink *displayLink;

@end

@implementation LocationViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
    _musicImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0 , LRLCurrentScreenWidth, LRLCurrentScreenHeight)];
    _musicImage.image = [UIImage imageNamed:@"backImage"];
    
    [self.view addSubview:_musicImage];
    [self configCurveView];
    [self updateShapeLayerPath];
    self.myLayer = _musicImage.layer;
    
    CABasicAnimation *anima=[CABasicAnimation animation];
    anima.keyPath=@"path";
    UIBezierPath *toPath = [self toPath:0];
    UIBezierPath *fromPath = [self toPath:LRLRadioWidth *-250];
    anima.fromValue = (id)fromPath.CGPath;
    anima.toValue = (id)toPath.CGPath;
    anima.duration = 1;
    anima.removedOnCompletion=NO;
    anima.fillMode=kCAFillModeForwards;
    [_musicImage.layer.mask addAnimation:anima forKey:nil];

    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(calculatePath)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    // 结束的时候调用calculatePath
    _displayLink.paused = YES;
    
    _leftY = 250 *LRLRadioHeight;
    _rightY = 250 *LRLRadioHeight;
    _curveY = 290  *LRLRadioHeight;
    _maskview = [[UIView alloc]init];
    _maskview.frame = CGRectMake(_curveX, _curveY, 3, 3);
    _maskview.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    [self.view addSubview:_maskview];
      [self performSelector:@selector(duang) withObject:nil afterDelay:1];
    
}



- (void)configCurveView
{
    _curveX = LRLCurrentScreenWidth/2.0;
    _curveY = 0;
    _leftY = 0;
    _rightY = 0;
}

- (void)updateShapeLayerPath{
    
    UIBezierPath *tPath = [UIBezierPath bezierPath];
    [tPath moveToPoint:CGPointMake(0, 0)];  //r1点
    [tPath addLineToPoint:CGPointMake(LRLCurrentScreenWidth, 0)];// r2点
    [tPath addLineToPoint:CGPointMake(LRLCurrentScreenWidth, _rightY)]; //r4点
    [tPath addQuadCurveToPoint:CGPointMake(0, _leftY)
                  controlPoint:CGPointMake(_curveX, _curveY)]; // r3,r4,r5确定的一个弧线
    [tPath closePath];
    _maskLayer = [[CAShapeLayer alloc] init];
    _maskLayer.frame = CGRectMake(0, 0 , LRLCurrentScreenWidth, 140 *LRLRadioHeight);
    [_maskLayer removeFromSuperlayer];
    _maskLayer.path = tPath.CGPath;
    _musicImage.layer.mask = _maskLayer;

}

- (void)duang{
    [UIView animateWithDuration:0.1 animations:^{
        _displayLink.paused = NO;
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.5
                              delay:0
             usingSpringWithDamping:0.3
              initialSpringVelocity:1
                            options:0
                         animations:^{
                             _maskview.frame = LRLCGRectMake(160, 270,3, 3);                      
                         }
                         completion:nil];
    }];
}

- (void)calculatePath
{
    CALayer *layer = _maskview.layer.presentationLayer;
    _curveX = layer.position.x;
    _curveY = layer.position.y;
    _leftY = layer.position.y - 40;
    _rightY = layer.position.y - 40;
    [self updateShapeLayerPath];
}

- (UIBezierPath *)toPath:(CGFloat )max_y{
    UIBezierPath *toPath =[UIBezierPath bezierPath];
    [toPath moveToPoint:CGPointMake(0, 0)];  //r1点
    [toPath addLineToPoint:CGPointMake(LRLCurrentScreenWidth, 0)];// r2点
    [toPath addLineToPoint:CGPointMake(LRLCurrentScreenWidth,250 *LRLRadioHeight + max_y)]; //r4点
    [toPath addQuadCurveToPoint:CGPointMake(0,250 *LRLRadioHeight + max_y)
                   controlPoint:CGPointMake(LRLCurrentScreenWidth/2, 290 *LRLRadioHeight + max_y)]; // r3,r4,r5确定的一个弧线
    [toPath closePath];
    return toPath;

}
@end
